package cn.hh.org.service.Impl;

import cn.hh.basic.service.BaseService;
import cn.hh.basic.service.impl.BaseServiceImpl;
import cn.hh.org.domain.Employee;
import cn.hh.org.mapper.EmployeeMapper;
import cn.hh.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     *  添加角色
     * @param employee
     */
    @Override
    public void add(Employee employee) {

        super.add(employee);

        // 判断角色是否为空,不为空代表员工选了角色的
        if (employee.getRole_id() != null){
            // 添加员工id,员工的角色id
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }
    }

    /**
     *  删除角色
     * @param id
     */
    @Override
    public void del(Long id) {
        // 传入员工id,删除角色
        employeeMapper.delEmployeeRole(id);

        super.del(id);
    }


    /**
     * 修改角色
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        // 先删角色,传入员工id
        employeeMapper.delEmployeeRole(employee.getId());
        // 再添加员工id,角色id
        if (employee.getRole_id() != null){
            employeeMapper.addEmployeeRole(employee.getId(),employee.getRole_id());
        }
        super.update(employee);
    }


    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
}
